import { createStore } from "vuex";
import router from "../router";
import config from "../config/index.js";
import { common } from "../request/api.js";
const mutations = {
  // 保存token
  setToken(state, value) {
    state.token = value;
    localStorage.setItem("token", value);
  },
  // 保存token
  loginOut(state, value) {
    state.token = "";
    localStorage.removeItem("token");
  },
  // 清除token
  removeToken(state) {
    state.token = "";
    state.userInfo = {};
    state.authList = [];
    localStorage.removeItem("token");
    localStorage.removeItem("authList");
    localStorage.removeItem("userInfo");
    location.href = location.href + "";
  },
  // 保存个人信息
  setUserInfo(state, value) {
    state.userInfo = value;
    localStorage.setItem("userInfo", JSON.stringify(value));
  },
  // 保存个人信息
  getUserInfo(state) {
    return state.userInfo;
  },
  // 清除个人信息
  removeUserInfo(state) {
    state.userInfo = null;
    localStorage.removeItem("userInfo");
  },
  //顶部导航栏添加
  manageTagListAdd(state, data) {
    let shielding = ["/login", "/view404"];
    if (shielding.indexOf(data.fullPath) == -1) {
      let isHas = false;
      for (let i in state.manageTagList) {
        if (state.manageTagList[i].name == data.name) {
          isHas = i;
          break;
        }
      }
      if (isHas === false) {
        state.manageTagList.push(data);
      } else {
        state.manageTagList[isHas].query = data.query;
      }
    }
  },
  // 顶部导航栏删除
  manageTagListRemove(state, data) {
    let index = -1;
    if (data.type != "all") {
      for (let i in state.manageTagList) {
        if (data.item.name == state.manageTagList[i].name) {
          index = i;
          break;
        }
      }
      if (index < 0) {
        return;
      }
    }

    switch (data.type) {
      case "all":
        state.manageTagList = [];
        break;
      case "right":
        if (state.manageTagList.length - 1 > index) {
          state.manageTagList.splice(index + 1);
        }
        break;
      case "this":
        state.manageTagList.splice(index, 1);
        if (data.route.name == data.item.name) {
          router.push(
            state.manageTagList[
              index >= state.manageTagList.length ? index - 1 : index
            ]
          );
        }
        break;
      default:
        break;
    }
    if (state.manageTagList.length == 0) {
      state.manageTagList.push(data.route);
      router.push(data.route);
    }
  },
  // 获取登陆账号权限
  setAuth(state, authList) {
    state.authList = authList;
    localStorage.setItem("authList", JSON.stringify(authList));
  },
  // 页面跳转
  $navTo(state, data) {
    router.push(data);
  },
  setCid(state, data) {
    state.cid = data;
  },
  setDeviceType(state, data) {
    state.deviceType = data;
  },
};
const state = {
  projectName: "致清坊",
  token: localStorage.getItem("token") || "",
  cid: "",
  mainColor: "#1d285f",
  userInfo: localStorage.getItem("userInfo")
    ? JSON.parse(localStorage.getItem("userInfo"))
    : {},
  manageTagList: [],
  webUrl: config.baseUrl,
  authList: localStorage.getItem("authList")
    ? JSON.parse(localStorage.getItem("authList"))
    : [],
  deviceType: "pc", //设备类型
};
const actions = {
  unLogin(state, that) {
    that
      .$confirm("确定退出此账号?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        lockScroll: false,
      })
      .then(() => {
        state.token = "";
        localStorage.removeItem("token");
        localStorage.removeItem("userInfo");
        router.push("/login");
      });
  },
  getWebUrl(state, url = "") {
    return `${url.indexOf("http") != -1 ? "" : config.baseUrl}${
      url[0] == "/" ? url : "/" + url
    }`;
  },
  getTime(state, { date = new Date().getTime(), type = "YYYY年MM月DD日" }) {
    function add0(str) {
      let num = Number(str);
      return num > 9 ? num + "" : "0" + num;
    }

    let WTxt = ["日", "一", "二", "三", "四", "五", "六"];
    let time = new Date(date),
      Y = time.getFullYear(),
      M = time.getMonth() + 1,
      D = time.getDate(),
      W = time.getDay(),
      h = time.getHours(),
      m = time.getMinutes(),
      s = time.getSeconds();
    let str = type
      .replace(/YYYY/g, add0(Y))
      .replace(/MM/g, add0(M))
      .replace(/DD/g, add0(D))
      .replace(/WW/g, WTxt[W])
      .replace(/hh/g, add0(h))
      .replace(/mm/g, add0(m))
      .replace(/ss/g, add0(s));
    return str;
  },
};
export default createStore({
  state,
  getters: {
   getDeviceType: (state) => {
      return state.deviceType;
    },
  },
  mutations,
  actions,
  modules: {},
});
